<?php
namespace app\index\controller;


use think\Controller;

use think\Db;

class Index extends Controller
{
    public function index()
    {
        // $data = Db::table('nideshop_ad')->select();

        // echo json_encode($data,256);
        $this->assign('name',"php");

        $this->assign('name1',"hello world");

        $this->assign("href","http://wwww.baidu.com");
        return $this->fetch();
    
    }

   public function text() //解决array转json字符串出现的乱码问题；
   {     /*思路一：直接在json_encode()函数的第二个参数加--JSON_UNESCAPED_UNICODE或256

         */
        $data = ['name'=>'thinkphp','url'=>'thinkphp.cn'];
        return json_encode(['data'=>$data,'code'=>1,'message'=>'操作完成'],256);
        
        /*思路二：
        1.在json_encode() 之前，先把中文用urlencode()函数转url编码( 中文变成%XXX的形式了)。
        2.在json_encode() 之后，把url编码的中文利用urldecode()函数转成中文


         foreach ( $data as $key => $value ) {  
            $data[$key] = urlencode ( $value );  
        }  
        echo urldecode ( json_encode ( $data ) );  
        */

   }
   public function getDate()
   {
       $data = Db::name('nideshop_ad')->select();

       return json_encode($data,256);
   }
}
